<?php
/**
 * comments.php
 *
 * @copyright 2018 opencart.cn
 *
 * All Rights Reserved
 * @link http://guangdawangluo.com
 *
 * @author StifferChen <chenlin@opencart.cn>
 * @created 2018-05-17 16:51
 * @modified 2018-05-17 16:51
 */

class ModelAccountComments extends Model
{
    public function addComments($data)
    {
        $sql = "insert into " . DB_PREFIX . "product_comments set order_id=" . (int)$data['order_id'] . ", product_id = " . (int)$data['product_id'] . ", product_name = ' " . $this->db->escape($data['product_name']) . "', product_model = '" . $this->db->escape($data['product_model']) . "', rating = '" . (int)$data['rating'] . "', comments = '" . $this->db->escape($data['comments']) . "', customer_id=" . (int)$this->customer->getId() . ", customer_name = '" . $this->customer->getFirstname() . "', date_created = now(), date_modified = NOW()";
        $this->db->query($sql);
        $comment_id = $this->db->getLastId();
        if (isset($data['images']) && $data['images']) {
            foreach ($data['images'] as $image) {
                $image_sql = "insert into " . DB_PREFIX . "product_comments_image set comments_id = " . (int)$comment_id . ", image = '" . $this->db->escape($image) . "'";
                $this->db->query($image_sql);
            }
        }
    }

    public function commentsTotal()
    {
        $row = $this->db->query("select count(*) total from " . DB_PREFIX . "product_comments where customer_id=" . (int)$this->customer->getId())->row;
        return $row ? $row['total'] : 0;
    }

    public function getComments($filter_data)
    {
        $sql = "select * from " . DB_PREFIX . "product_comments where customer_id=" . (int)$this->customer->getId();
        if ($filter_data['filter_order_id']) {
            $sql .= " and order_id=" . (int)$filter_data['filter_order_id'];
        }

        if ($filter_data['filter_rating'] != '') {
            $sql .= " and rating=" . (int)$filter_data['filter_rating'];
        }

        if ($filter_data['filter_date_created']) {
            $sql .= " and date_created='" . $this->db->escape($filter_data['filter_date_created']) . "'";
        }

        if ($filter_data['filter_date_modified']) {
            $sql .= " and date_modified='" . $this->db->escape($filter_data['filter_date_modified']) . "'";
        }

        if ($filter_data['filter_status']) {
            $sql .= " and status='" . $this->db->escape($filter_data['filter_status']) . "'";
        }

        $sql .= " order by date_created desc limit {$filter_data['start']}, {$filter_data['limit']}";

        return $this->db->query($sql)->rows;
    }

    public function getComment($id)
    {
        $comment = array();
        $comment_images = array();
        $main = $this->db->query("select * from " . DB_PREFIX . "product_comments where customer_id=" . (int)$this->customer->getId() . " and id=" . (int)$id)->row;
        if ($main) {
            $images = $this->db->query("select * from " . DB_PREFIX . "product_comments_image where comments_id=" . (int)$id)->rows;
            if ($images) {
                foreach ($images as $image) {
                    $comment_images[] = $image['image'];
                }
            }
            $main['images'] = $comment_images;
            $comment = $main;
        }
        return $comment;
    }

    public function deleteComment($id)
    {
        $this->db->query('delete from ' . DB_PREFIX . 'product_comments where id=' . (int)$id);
        $this->db->query('delete from ' . DB_PREFIX . 'product_comments_image where comments_id=' . (int)$id);
    }

    public function editComments($id, $data)
    {
        $comment = $this->getComment($id);
        if ($comment) {
            $this->db->query("update " . DB_PREFIX . "product_comments set rating=" . (int)$data['rating'] . ", comments='" . $this->db->escape($data['comments']) . "' where id=" . (int)$id);
            $this->db->query('delete from ' . DB_PREFIX . 'product_comments_image where comments_id=' . (int)$id);
            if ($data['images']) {
                foreach ($data['images'] as $image) {
                    $image_sql = "insert into " . DB_PREFIX . "product_comments_image set comments_id = " . (int)$id . ", image = '" . $this->db->escape($image) . "'";
                    $this->db->query($image_sql);
                }
            }
        }
    }
}